//
//  ZYStatusCell.swift
//  weibo007
//
//  Created by 陈宗毅 on 15/12/20.
//  Copyright © 2015年 czy. All rights reserved.
//

import UIKit

class ZYStatusCell: UITableViewCell {

    //配图宽度约束
    var pictureViewWidthCon:NSLayoutConstraint?
    //配图高度约束
    var pictureViewHeightCon:NSLayoutConstraint?

    //cell显示内容需要模型
    var status:ZYStatus?{
        didSet {
            // 当控制器给cell设置模型后,顶部视图显示数据也是依赖模型
            topView.status = status
            contentLabel.text  = status?.text
            // 将模型设置给配图视图.
            pictureView.status = status
            // 调用配图视图的计算尺寸的方法
            let size = pictureView.calcViewSize()
            //修改配图的高度
            pictureViewHeightCon?.constant = size.height
            pictureViewWidthCon?.constant = size.width
            
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        preapreUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func rowHeight(status:ZYStatus) ->CGFloat {
        self.status = status
        //更新布局
        layoutIfNeeded()
        //获取底部视图的最大Y值
        let maxY = CGRectGetMaxY(statusBottomView.frame)
        return maxY
    }
    
    
     func  preapreUI() {
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(statusBottomView)
        contentView.addSubview(pictureView)
        //添加约束
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53))
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: 8, y: 8))
        //微博宽度的约束
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: UIScreen.mainScreen().bounds.width - 2 * 8))
//        //微博配图
//        let cons = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width: 290, height: 290), offset: CGPoint(x: 0, y: 8))
        
//        pictureViewWidthCon = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
//        pictureViewHeightCon = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
        
        
        //底部视图
        statusBottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: -8, y: 8))
        
        
        //tableView cell的高度，想通过cell里面的内容来自适应，需要给contentview添加顶部和底部约束，让contentView的高度参照里面的内容来自动更改高度（需要添加顶部和底部约束）
        //添加contentView底部约束,contentView底部和contentLabel底部重合
//       contentView.addConstraint( NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: statusBottomView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
    }
    
    private lazy var topView:ZYTopView = ZYTopView()

    //微博文本内容
     lazy var contentLabel:UILabel = {
        let label = UILabel()
        
        label.numberOfLines = 0
        label.textColor = UIColor.darkGrayColor()
        //TODO:设置字体
        label.font = UIFont.systemFontOfSize(14)
        return label
    }()
    
    //微博配图视图
     lazy var pictureView:ZYStatusPictureView = ZYStatusPictureView()
    
     lazy var statusBottomView:ZYStatusBottomView = ZYStatusBottomView()

}
